package com.simple.core.dto;

import com.simple.core.enums.ResponseCode;
import com.simple.core.exception.BizException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 响应对象
 *
 * @author FanXing
 * @date 2022年11月04日 16:05
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Response<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private Boolean success;

    private T data;

    private String message;

    private Integer code;

    public static <T> Response<T> ok() {
        return new ResponseBuilder<T>()
                .success(Boolean.TRUE)
                .code(ResponseCode.SUCCESS.getCode())
                .build();
    }

    public static <T> Response<T> ok(T data) {
        return new ResponseBuilder<T>()
                .success(Boolean.TRUE)
                .code(ResponseCode.SUCCESS.getCode())
                .data(data)
                .build();
    }


    public static <T> Response<T> error(String message) {
        return new ResponseBuilder<T>()
                .success(Boolean.FALSE)
                .code(ResponseCode.FAIL.getCode())
                .message(message)
                .build();
    }

    public static <T> Response<T> error(String message, T data) {
        return new ResponseBuilder<T>()
                .success(Boolean.FALSE)
                .code(ResponseCode.FAIL.getCode())
                .message(message)
                .data(data)
                .build();
    }

    public static <T> Response<T> error(ResponseCode responseCode) {
        return new ResponseBuilder<T>()
                .success(Boolean.FALSE)
                .code(responseCode.getCode())
                .message(responseCode.getMessage())
                .build();
    }

    public static <T> Response<T> error(ResponseCode responseCode, T data) {
        return new ResponseBuilder<T>()
                .success(Boolean.FALSE)
                .code(responseCode.getCode())
                .message(responseCode.getMessage())
                .data(data)
                .build();
    }

    public static <T> Response<T> of(ResponseCode responseCode) {
        return new ResponseBuilder<T>()
                .success(responseCode.getCode() == ResponseCode.SUCCESS.getCode() ? Boolean.TRUE : Boolean.FALSE)
                .code(responseCode.getCode())
                .message(responseCode.getMessage())
                .build();
    }

    public static <T> Response<T> of(Boolean success) {
        return new ResponseBuilder<T>()
                .success(success)
                .code(success ? ResponseCode.SUCCESS.getCode() : ResponseCode.FAIL.getCode())
                .build();
    }

    public static <T> Response<T> of(Boolean success, ResponseCode responseCode) {
        if (success && responseCode.getCode() != ResponseCode.SUCCESS.getCode()) {
            throw new BizException("code冲突");
        }
        return new ResponseBuilder<T>()
                .success(success)
                .code(success ? ResponseCode.SUCCESS.getCode() : responseCode.getCode())
                .build();
    }

    public static <T> Response<T> of(Boolean success, String message) {
        return new ResponseBuilder<T>()
                .success(success)
                .code(success ? ResponseCode.SUCCESS.getCode() : ResponseCode.FAIL.getCode())
                .message(message)
                .build();
    }
}
